import { Component, Vue, Inject } from 'vue-property-decorator';

import { maxLength } from 'vuelidate/lib/validators';

import AlertService from '@/shared/alert/alert.service';

import { IRoomBasicProperty, RoomBasicProperty } from '@/shared/model/room-basic-property.model';
import RoomBasicPropertyService from './room-basic-property.service';

const validations: any = {
  roomBasicProperty: {
    houseType: {
      maxLength: maxLength(32),
    },
    buildingSize: {
      maxLength: maxLength(32),
    },
    structure: {
      maxLength: maxLength(16),
    },
    roomFloor: {
      maxLength: maxLength(32),
    },
    buildingType: {
      maxLength: maxLength(32),
    },
    ladderHouseHold: {
      maxLength: maxLength(16),
    },
    floorStructure: {
      maxLength: maxLength(16),
    },
    roomTowards: {
      maxLength: maxLength(8),
    },
    roomFitment: {
      maxLength: maxLength(8),
    },
    ladderHouseholds: {
      maxLength: maxLength(16),
    },
    lift: {},
  },
};

@Component({
  validations,
})
export default class RoomBasicPropertyUpdate extends Vue {
  @Inject('roomBasicPropertyService') private roomBasicPropertyService: () => RoomBasicPropertyService;
  @Inject('alertService') private alertService: () => AlertService;

  public roomBasicProperty: IRoomBasicProperty = new RoomBasicProperty();
  public isSaving = false;
  public currentLanguage = '';

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.params.roomBasicPropertyId) {
        vm.retrieveRoomBasicProperty(to.params.roomBasicPropertyId);
      }
    });
  }

  created(): void {
    this.currentLanguage = this.$store.getters.currentLanguage;
    this.$store.watch(
      () => this.$store.getters.currentLanguage,
      () => {
        this.currentLanguage = this.$store.getters.currentLanguage;
      }
    );
  }

  public save(): void {
    this.isSaving = true;
    if (this.roomBasicProperty.id) {
      this.roomBasicPropertyService()
        .update(this.roomBasicProperty)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = 'A RoomBasicProperty is updated with identifier ' + param.id;
          return this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Info',
            variant: 'info',
            solid: true,
            autoHideDelay: 5000,
          });
        })
        .catch(error => {
          this.isSaving = false;
          this.alertService().showHttpError(this, error.response);
        });
    } else {
      this.roomBasicPropertyService()
        .create(this.roomBasicProperty)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = 'A RoomBasicProperty is created with identifier ' + param.id;
          this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Success',
            variant: 'success',
            solid: true,
            autoHideDelay: 5000,
          });
        })
        .catch(error => {
          this.isSaving = false;
          this.alertService().showHttpError(this, error.response);
        });
    }
  }

  public retrieveRoomBasicProperty(roomBasicPropertyId): void {
    this.roomBasicPropertyService()
      .find(roomBasicPropertyId)
      .then(res => {
        this.roomBasicProperty = res;
      })
      .catch(error => {
        this.alertService().showHttpError(this, error.response);
      });
  }

  public previousState(): void {
    this.$router.go(-1);
  }

  public initRelationships(): void {}
}
